package com.ljw.community.controller;

import com.ljw.community.entity.AjaxRespInfo;
import com.ljw.community.entity.User;
import com.ljw.community.service.CommonService;
import com.ljw.community.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userController")
public class UserController extends BasicController{

    @Resource
    private CommonService commonService;

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    ModelAndView login(User user) {
        ModelAndView mv = new ModelAndView();
        //登录名密码空
        if (user.getLoginName().isEmpty() || user.getPassword().isEmpty()) {
            mv.addObject("errMsg", "登录名或密码不能为空");
            mv.setViewName("/user/login");
            return mv;
        }
        String msg = commonService.login(user.getLoginName(), user.getPassword(), "user");
        if (msg.isEmpty()) {
            mv.setViewName("redirect:/user/index?loginName=" + user.getLoginName());
        } else {
            mv.addObject("errMsg", msg);
            mv.setViewName("/user/login");
        }
        return mv;
    }

    @RequestMapping("/register")
    @ResponseBody
    AjaxRespInfo register(@RequestBody User user) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        String msg = "";
        if (user == null) {
            msg = "错误";
        } else {
            msg = userService.register(user);
            ajaxRespInfo.setCode(0);
        }
        ajaxRespInfo.setMsg(msg);

        return ajaxRespInfo;
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    AjaxRespInfo<User> getInfo(String loginName) {
        AjaxRespInfo<User> ajaxRespInfo = new AjaxRespInfo<>();
        String msg = "";
        if (loginName == null || loginName.isEmpty()) {
            msg = "错误";
        } else {
            User dbUser = userService.getInfo(loginName);
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(dbUser);
        }
        ajaxRespInfo.setMsg(msg);

        return ajaxRespInfo;
    }


    @RequestMapping("/modifyInfo")
    @ResponseBody
    AjaxRespInfo<User> modifyInfo(@RequestBody User user) {
        AjaxRespInfo<User> ajaxRespInfo = new AjaxRespInfo<>();
        int rowNum = userService.update(user);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("修改成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

    @RequestMapping("/changeProfile")
    @ResponseBody
    AjaxRespInfo<User> changeProfile(MultipartFile imgFile, String loginName) {
        AjaxRespInfo<User> ajaxRespInfo = new AjaxRespInfo<>();
        String imgUrl = uploadFile("profile/", imgFile);
        User user = new User();
        user.setImg(imgUrl);
        user.setLoginName(loginName);
        int rowNum = userService.update(user);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }


}
